package com.goalias.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {


    public static ThreadPoolExecutor goaliasThreadPool = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors(),
            Runtime.getRuntime().availableProcessors() * 2,
            1,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(),
            ThreadFactoryBuilder.create().setNamePrefix("goalias thread-").build(),
            new ThreadPoolExecutor.CallerRunsPolicy());

   public static ThreadPoolExecutor bizAsyncThreadPool = new ThreadPoolExecutor(
           Runtime.getRuntime().availableProcessors(),
           Runtime.getRuntime().availableProcessors() * 2,
            1,
           TimeUnit.SECONDS,
           new LinkedBlockingQueue<>(),
           ThreadFactoryBuilder.create().setNamePrefix("bizAsync thread-").build(),
           new ThreadPoolExecutor.CallerRunsPolicy());
   public  static  ScheduledThreadPoolExecutor goaliasScheduledThreadPool = new ScheduledThreadPoolExecutor(5);
    @Override
    public Executor getAsyncExecutor() {
        return bizAsyncThreadPool;
    }
}
